package com.ruoyi.mp.controller;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mp.domain.MpAutoReply;
import com.ruoyi.mp.service.IMpAccountService;
import com.ruoyi.mp.service.IMpAutoReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 自动回复Controller
 *
 * @author 郎兴宇
 * @date 2023-11-18
 */
@RestController
@RequestMapping("/reply")
public class MpAutoReplyController extends BaseController
{
    @Autowired
    private IMpAutoReplyService mpAutoReplyService;
    @Autowired
    private IMpAccountService iMpAccountService;
    /**
     * 查询自动回复列表
     */
    @RequiresPermissions("mp:reply:list")
    @GetMapping("/list")
    public TableDataInfo list(MpAutoReply mpAutoReply)
    {
        startPage();
        List<MpAutoReply> list = mpAutoReplyService.selectMpAutoReplyList(mpAutoReply);
        return getDataTable(list);
    }

    /**
     * 导出自动回复列表
     */
    @RequiresPermissions("mp:reply:export")
    @Log(title = "自动回复", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MpAutoReply mpAutoReply)
    {
        List<MpAutoReply> list = mpAutoReplyService.selectMpAutoReplyList(mpAutoReply);
        ExcelUtil<MpAutoReply> util = new ExcelUtil<MpAutoReply>(MpAutoReply.class);
        util.exportExcel(response, list, "自动回复数据");
    }

    /**
     * 获取自动回复详细信息
     */
    @RequiresPermissions("mp:reply:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(mpAutoReplyService.selectMpAutoReplyById(id));
    }

    /**
     * 新增自动回复
     */
    @RequiresPermissions("mp:reply:add")
    @Log(title = "自动回复", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MpAutoReply mpAutoReply)
    {
        mpAutoReply.setAppId(this.iMpAccountService.selectMpAccountById(mpAutoReply.getAccountId()).getAppId());
        return toAjax(mpAutoReplyService.insertMpAutoReply(mpAutoReply));
    }

    /**
     * 修改自动回复
     */
    @RequiresPermissions("mp:reply:edit")
    @Log(title = "自动回复", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MpAutoReply mpAutoReply)
    {
        return toAjax(mpAutoReplyService.updateMpAutoReply(mpAutoReply));
    }

    /**
     * 删除自动回复
     */
    @RequiresPermissions("mp:reply:remove")
    @Log(title = "自动回复", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mpAutoReplyService.deleteMpAutoReplyByIds(ids));
    }
}
